package com.example.ctrip.utils;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * don't ask me how it works,I don't know either！WTF！
 */
public class SeleniumUtil {

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.firefox.bin","F:/FireFox/firefox.exe");
        WebDriver webDriver = new FirefoxDriver();
        webDriver.get("https://www.chinaexpressair.com/yss/login/");
        webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        System.out.println(webDriver.getTitle());
        System.out.println(webDriver.getWindowHandle());
        webDriver.findElement(By.id("pwdLoginUsername")).sendKeys("13512338032");
        webDriver.findElement(By.id("pwdLoginPassword")).sendKeys("duyajun@8032");
        webDriver.findElement(By.id("pwdLoginBtn")).click();

        Thread.sleep(2000);
        System.out.println("imgsrc ==========="+webDriver.findElement(By.className("yidun_bg-img")).getAttribute("src"));
        String url = webDriver.findElement(By.className("yidun_bg-img")).getAttribute("src");
        String url1 = webDriver.findElement(By.className("yidun_jigsaw")).getAttribute("src");
        System.out.println("url ======================"+url);
        String[] split = url.split("/");
        String fileName = split[split.length - 1];
        File file = new File("D:/imgtest/"+fileName);
        InputStream inputStream = ImageUtil
                .getInputStreamByGet(url);
        ImageUtil.saveData(inputStream, file);
        ImageUtil.convert("D:/imgtest/"+fileName,"png","D:/imgtest/one.png");

        String[] split1 = url1.split("/");
        String fileName1 = split1[split1.length - 1];
        File file1 = new File("D:/imgtest/"+fileName1);
        InputStream inputStream1 = ImageUtil
                .getInputStreamByGet(url1);
        ImageUtil.saveData(inputStream1, file1);
        ImageUtil.convert("D:/imgtest/"+fileName1,"png","D:/imgtest/two.png");

        double distance = OpenCvUtil.getDistance("file:///D:/imgtest/one.png","file:///D:/imgtest/two.png");
//        file.delete();
//        file1.delete();
//        new File("F:/imgtest/one.png").delete();
//        new File("F:/imgtest/two.png").delete();
        System.out.println("distance==========="+distance);

        System.out.println("postContainer"+webDriver.findElement(By.id("pwdLoginBtn")).getText());
        move(webDriver,webDriver.findElement(By.className("yidun_jigsaw")), (int) distance+18);

    }
    /**
            * 模拟人工移动
	 * * @param driver
	 * * @param element页面滑块
	 * * @param distance需要移动距离
	 */
    public static void move(WebDriver driver, WebElement element, int distance) throws InterruptedException {
        int randomTime = 0;
        if (distance > 90) {
            randomTime = 250;
        } else if (distance > 80 && distance <= 90) {
            randomTime = 150;
        }
        List<Integer> track = getMoveTrack(distance - 2);
        int moveY = 1;
        try {
            Actions actions = new Actions(driver);
            actions.clickAndHold(element).perform();
            Thread.sleep(200);
            for (int i = 0; i < track.size(); i++) {
                actions.moveByOffset(track.get(i), moveY).perform();
//                Thread.sleep(new Random().nextInt(300) + randomTime);
            }
//            Thread.sleep(200);
            actions.release(element).perform();
//            actions.release(element);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 根据距离获取滑动轨迹
     * * @param distance需要移动的距离
     * @return
     */
    public static List<Integer> getMoveTrack(int distance) {
        List<Integer> track = new ArrayList<>();// 移动轨迹
        Random random = new Random();
        int current = 0;// 已经移动的距离
        int mid = (int) distance * 4 / 5;// 减速阈值
        int a = 0;
        int move = 0;// 每次循环移动的距离
        while (true) {
            a = random.nextInt(10);
            if (current <= mid) {
                move += a;// 不断加速
            } else {
                move -= a;
            }
            if ((current + move) < distance) {
                track.add(move);
            } else {
                track.add(distance - current);
                break;
            }
            current += move;
        }
        return track;
    }
}
